在android中创建一个http请求它停止并且应用程序强制关闭

您所在的位置:网站首页 android app 数据库 在android中创建一个http请求它停止并且应用程序强制关闭

在android中创建一个http请求它停止并且应用程序强制关闭

2023-03-14 18:16| 来源: 网络整理| 查看: 265

我想在android上使用JSON和php/mysql的HTTP请求来显示mysql数据库中的一些数据。在android中创建一个http请求它停止并且应用程序强制关闭

的应用只需按下一个按钮,它会在日志猫从数据库中显示所选择的数据,并将其使用意向显示模拟器上的第二页..

的问题是,当我尝试运行该应用程序之前,该系统压碎将请求发送到HTTP和我使用本地主机服务器(瓦帕),所以我使用的URL为:http:// 10.0.2.2/studentservice/StudentService/getStudentByID.php

我按下按钮后,这是

logCat 10-15 14:07:28.160: I/dalvikvm(854): threadid=3: reacting to signal 3 10-15 14:07:28.320: I/dalvikvm(854): Wrote stack traces to '/data/anr/traces.txt' 10-15 14:07:28.630: D/gralloc_goldfish(854): Emulator without GPU emulation detected. 10-15 14:07:28.660: I/dalvikvm(854): threadid=3: reacting to signal 3 10-15 14:07:28.670: I/dalvikvm(854): Wrote stack traces to '/data/anr/traces.txt' 10-15 14:07:35.800: E/Error befor http(854): Error before the http request 10-15 14:07:36.210: I/dalvikvm(854): threadid=3: reacting to signal 3 10-15 14:07:36.230: I/dalvikvm(854): Wrote stack traces to '/data/anr/traces.txt' 10-15 14:07:36.730: I/dalvikvm(854): threadid=3: reacting to signal 3 10-15 14:07:36.750: I/dalvikvm(854): Wrote stack traces to '/data/anr/traces.txt' 10-15 14:07:37.250: I/dalvikvm(854): threadid=3: reacting to signal 3 10-15 14:07:37.270: I/dalvikvm(854): Wrote stack traces to '/data/anr/traces.txt' 10-15 14:07:37.770: I/dalvikvm(854): threadid=3: reacting to signal 3 10-15 14:07:37.780: I/dalvikvm(854): Wrote stack traces to '/data/anr/traces.txt' 10-15 14:07:38.281: I/dalvikvm(854): threadid=3: reacting to signal 3 10-15 14:07:38.300: I/dalvikvm(854): Wrote stack traces to '/data/anr/traces.txt' 10-15 14:07:38.800: I/dalvikvm(854): threadid=3: reacting to signal 3 10-15 14:07:38.821: I/dalvikvm(854): Wrote stack traces to '/data/anr/traces.txt' 10-15 14:07:48.400: I/dalvikvm(854): threadid=3: reacting to signal 3 10-15 14:07:48.460: I/dalvikvm(854): Wrote stack traces to '/data/anr/traces.txt' 10-15 14:10:25.630: I/dalvikvm(854): threadid=3: reacting to signal 3 10-15 14:10:25.650: I/dalvikvm(854): Wrote stack traces to '/data/anr/traces.txt' 后粉碎日志猫显示 10-15 14:35:13.781: E/AndroidRuntime(906): FATAL EXCEPTION: main 10-15 14:35:13.781: E/AndroidRuntime(906): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lebdev.fitguide.activities/com.lebdev.fitguide.activities.SecondaryActivity}: java.lang.NullPointerException: println needs a message 10-15 14:35:13.781: E/AndroidRuntime(906): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 10-15 14:35:13.781: E/AndroidRuntime(906): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 10-15 14:35:13.781: E/AndroidRuntime(906): at android.app.ActivityThread.access$600(ActivityThread.java:123) 10-15 14:35:13.781: E/AndroidRuntime(906): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 10-15 14:35:13.781: E/AndroidRuntime(906): at android.os.Handler.dispatchMessage(Handler.java:99) 10-15 14:35:13.781: E/AndroidRuntime(906): at android.os.Looper.loop(Looper.java:137) 10-15 14:35:13.781: E/AndroidRuntime(906): at android.app.ActivityThread.main(ActivityThread.java:4424) 10-15 14:35:13.781: E/AndroidRuntime(906): at java.lang.reflect.Method.invokeNative(Native Method) 10-15 14:35:13.781: E/AndroidRuntime(906): at java.lang.reflect.Method.invoke(Method.java:511) 10-15 14:35:13.781: E/AndroidRuntime(906): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 10-15 14:35:13.781: E/AndroidRuntime(906): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 10-15 14:35:13.781: E/AndroidRuntime(906): at dalvik.system.NativeStart.main(Native Method) 10-15 14:35:13.781: E/AndroidRuntime(906): Caused by: java.lang.NullPointerException: println needs a message 10-15 14:35:13.781: E/AndroidRuntime(906): at android.util.Log.println_native(Native Method) 10-15 14:35:13.781: E/AndroidRuntime(906): at android.util.Log.e(Log.java:231) 10-15 14:35:13.781: E/AndroidRuntime(906): at com.lebdev.fitguide.controller.HttpManager.getResponseFromURL(HttpManager.java:70) 10-15 14:35:13.781: E/AndroidRuntime(906): at com.lebdev.fitguide.activities.SecondaryActivity.onCreate(SecondaryActivity.java:54) 10-15 14:35:13.781: E/AndroidRuntime(906): at android.app.Activity.performCreate(Activity.java:4465) 10-15 14:35:13.781: E/AndroidRuntime(906): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 10-15 14:35:13.781: E/AndroidRuntime(906): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 10-15 14:35:13.781: E/AndroidRuntime(906): ... 11 more 10-15 14:35:14.341: I/dalvikvm(906): threadid=3: reacting to signal 3 10-15 14:35:14.370: I/dalvikvm(906): Wrote stack traces to '/data/anr/traces.txt' MainActivity package com.lebdev.fitguide.activities; import com.lebdev.fitguide.businessModel.Student; import com.lebdev.fitguide.businessModel.Subject; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { EditText txtMessage; TextView lblMessage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtMessage = (EditText) findViewById(R.id.txtMessage); lblMessage = (TextView) findViewById(R.id.lblMessage); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public void Onclick_btnSend(View v) { Intent intent = new Intent(this, SecondaryActivity.class); //intent.putExtra("message", txtMessage.getText().toString()); Student std = new Student(txtMessage.getText().toString(),1234, 1); std.getSubjects().add(new Subject("itc", 5)); intent.putExtra("Student", std); startActivity(intent); Toast.makeText(this, "i am sending a message here", Toast.LENGTH_SHORT).show(); } } JsonObjectMapper package com.lebdev.fitguide.om; import java.util.ArrayList; import java.util.List; import org.json.JSONObject; import android.util.Log; import com.lebdev.fitguide.businessModel.Student; public class JsonObjectMapper { public static Student jsonToStudent(JSONObject jsonObject) { Student std = null; try { std = new Student(jsonObject.getJSONArray("Student") .getJSONObject(0).getString("Name"), Integer.parseInt(jsonObject.getJSONArray("Student") .getJSONObject(0).getString("index")), Integer.parseInt(jsonObject.getJSONArray("Student") .getJSONObject(0).getString("ID"))); } catch (Exception ex) { Log.e("Error Json Converter", ex.getMessage()); } return std; } public static List jsonToStudentList(JSONObject jsonObject) { List stdList = new ArrayList(); try { for (int i = 0; i < jsonObject.getJSONArray("Student").length(); i++) { stdList.add(new Student(jsonObject.getJSONArray("Student") .getJSONObject(i).getString("Name"), Integer .parseInt(jsonObject.getJSONArray("Student") .getJSONObject(i).getString("index")), Integer .parseInt(jsonObject.getJSONArray("Student") .getJSONObject(i).getString("ID")))); } } catch (Exception ex) { Log.e("Error Json Converter", ex.getMessage()); } return stdList; } } SecondaryObject package com.lebdev.fitguide.activities; import java.util.ArrayList; import java.util.List; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import org.json.JSONObject; import com.lebdev.fitguide.businessModel.Student; import com.lebdev.fitguide.controller.HttpManager; import com.lebdev.fitguide.controller.JSONParser; import com.lebdev.fitguide.om.JsonObjectMapper; import android.app.Activity; import android.os.Bundle; import android.os.StrictMode; import android.util.Log; import android.view.Menu; import android.widget.TextView; import android.widget.Toast; public class SecondaryActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_secondary); StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() .permitAll().build(); StrictMode.setThreadPolicy(policy); List params = new ArrayList(); params.add(new BasicNameValuePair("sid", "2")); Log.e("Error befor http", "Error before the http request"); JSONObject jsonObj = JSONParser .parseJSONFromString(new HttpManager() .getResponseFromURL( "http://10.0.2.2/studentservice/StudentService/getStudentByID.php", params)); Log.e("Error after http", "Error after the http request"); Student std = JsonObjectMapper.jsonToStudent(jsonObj); Log.e("STUDENT DATA", "ID:" + std.getID() + "Name:" + std.getName() + "Index: " + std.getIndex()); /* Bundle bundle = getIntent().getExtras(); if (bundle != null) { TextView lblMessage = (TextView) findViewById(R.id.lblSentMessage); Student student = bundle.getParcelable("Student"); lblMessage.setText("I 've recieved a student with a name " + student.getName() + " and has a Subject " + student.getSubjects().get(0).getName() + " with a grade " + student.getSubjects().get(0).getGrade()); } */ } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.secondary, menu); return true; } }

来源

2013-10-15 LebDev

+0

请发布LogCat和谷歌你的例外! –



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3